/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View;

import Control.SemanticQualificationController;
import Model.FormalConcept;
import java.awt.Component;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JPanel;

/**
 *
 * @author dark
 */
public class NewCP extends javax.swing.JFrame {

    /**
     * Creates new form NewCP
     */
    
    private static PrincipalWindowGUI pwg;
    private FormalConcept fc;
    
    
    public NewCP(){
        initComponents();
    }
            
    
    public NewCP(PrincipalWindowGUI gui, FormalConcept myFC) {
        initComponents();
        pwg = gui;
        fc = myFC;
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
    
    public javax.swing.JButton getJButtonCPConfirm(){
        return jButtonCPConfirm;
    }
    
    private static void addAButton(String text, Container container, FormalConcept fc) {
        MyMenuButton button = new MyMenuButton(text, fc);
        button.updateMenu(pwg.getSQC().textCP(fc));
        button.setAlignmentX(Component.CENTER_ALIGNMENT);
        button.setPWG(pwg);
        container.add(button);
    }
    
    public void displayFC(){
        // create the jpanel
        JPanel jp = new JPanel();
        jp.setLayout(new BoxLayout(jp, BoxLayout.Y_AXIS));
        // add buttons
        for (int i = 0; i<pwg.getSQC().FcOntCpt().size(); i++){
            System.out.println(pwg.getSQC().FcOntCpt().get(i));
            addAButton(pwg.getSQC().FcOntCpt().get(i), jp, pwg.getSQC().getFCS().get(i));
        }
        // display ScrollPane
        pwg.getJScrollPane4().getViewport().add(jp, null);
        pwg.doLayout();
        pwg.pack();
        pwg.doLayout();
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabelAtt = new javax.swing.JLabel();
        jLabelFF = new javax.swing.JLabel();
        jButtonCPConfirm = new javax.swing.JButton();
        jButtonCPCancel = new javax.swing.JButton();
        jButtonAtt = new javax.swing.JButton();
        jButtonFF = new javax.swing.JButton();
        jTextAtt = new javax.swing.JLabel();
        jTextFF = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("New Correspondence ");

        jPanel1.setBackground(new java.awt.Color(192, 184, 175));

        jLabelAtt.setText("Please select an attribute of the formal concept in ontology, then click on select");

        jLabelFF.setText("Please select a form field correspondant to the attribute above, then click on select");

        jButtonCPConfirm.setText("Confirm");
        jButtonCPConfirm.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCPConfirmActionPerformed(evt);
            }
        });

        jButtonCPCancel.setText("Cancel");
        jButtonCPCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCPCancelActionPerformed(evt);
            }
        });

        jButtonAtt.setText("Select");
        jButtonAtt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAttActionPerformed(evt);
            }
        });

        jButtonFF.setText("Select");

        jTextAtt.setText("??");

        jTextFF.setText("??");

        jLabel2.setText("Attribute selected : ");

        jLabel3.setText("Field selected : ");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabelAtt)
                            .addComponent(jLabelFF)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(45, 45, 45)
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jTextAtt)
                                .addGap(44, 44, 44)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addGap(102, 102, 102)
                                        .addComponent(jButtonAtt))
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addGap(103, 103, 103)
                                        .addComponent(jButtonFF))))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(54, 54, 54)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addGap(32, 32, 32)
                                .addComponent(jTextFF))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jButtonCPConfirm)
                                .addGap(72, 72, 72)
                                .addComponent(jButtonCPCancel)))))
                .addContainerGap(30, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabelAtt)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonAtt)
                    .addComponent(jTextAtt)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addComponent(jLabelFF)
                .addGap(4, 4, 4)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonFF)
                    .addComponent(jTextFF)
                    .addComponent(jLabel3))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonCPConfirm)
                    .addComponent(jButtonCPCancel))
                .addGap(0, 21, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addComponent(jLabel1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonCPConfirmActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCPConfirmActionPerformed
        // TODO add your handling code here:
        pwg.getSQC().createCorrespondenceControl(jTextFF.getText(),fc,jTextAtt.getText());
        pwg.getSQC().addCorrespondenceControl(fc, pwg.getSQC().getCP());
        pwg.getSQC().updateListFormalConceptControl(fc);
        pwg.getSQC().setCP();
        this.displayFC();
        this.setVisible(false);
        this.setEnabled(false);
        pwg.setEnabled(true);
    }//GEN-LAST:event_jButtonCPConfirmActionPerformed

    private void jButtonCPCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCPCancelActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
        this.setEnabled(false);
        pwg.setEnabled(true);
    }//GEN-LAST:event_jButtonCPCancelActionPerformed

    private void jButtonAttActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAttActionPerformed
        // TODO add your handling code here:
        jTextAtt.setText(pwg.getTextSelected());
    }//GEN-LAST:event_jButtonAttActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(NewCP.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(NewCP.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(NewCP.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(NewCP.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NewCP().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonAtt;
    private javax.swing.JButton jButtonCPCancel;
    private javax.swing.JButton jButtonCPConfirm;
    private javax.swing.JButton jButtonFF;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabelAtt;
    private javax.swing.JLabel jLabelFF;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel jTextAtt;
    private javax.swing.JLabel jTextFF;
    // End of variables declaration//GEN-END:variables
}
